package com.led_on_off.led;

import ohos.agp.components.*;
import ohos.agp.render.render3d.ViewHolder;
import ohos.app.Context;

import java.util.List;

public class ListProvider extends BaseItemProvider {
    LayoutScatter layoutScatter;
    private List<String> data;

    public ListProvider(Context context, List<String> data) {
        this.data = data;
        this.layoutScatter = LayoutScatter.getInstance(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        ViewHolder viewHolder = null;
        //component相当于Android中的view，其他的和Android中ListView的适配器adapter差不多。
        // 名字区别也不大，不过Android中ListView基本被淘汰了。
        if (component == null) {
            component = layoutScatter.parse(ResourceTable.Layout_list_container, null, false);
            viewHolder = new ViewHolder(component);
            component.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) component.getTag();
        }
        viewHolder.tvItemName.setText(data.get(i));
        return component;
    }

    static class ViewHolder {
        Text tvItemName;
        public ViewHolder(Component componentContainer) {
            tvItemName = (Text) componentContainer.findComponentById(ResourceTable.Id_listText);
        }
    }
}
